package cn.stronglink.assetmanage.business.check.view;

import android.app.Dialog;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.View;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.stronglink.assetmanage.MyApplication;
import cn.stronglink.assetmanage.R;
import cn.stronglink.assetmanage.business.check.adapter.CheckVpAdapter;
import cn.stronglink.assetmanage.utils.ToastUtil;
import cn.stronglink.assetmanage.widget.CustomProgressDialog;

/**
 * 操作入库主fragment
 * Created by chencongcong on 2018/6/12.
 */

public class CheckView {
    private Fragment fragment;
    @BindView(R.id.tb_check)
    TabLayout tb_check;
    @BindView(R.id.vp_check)
    ViewPager vp_check;
    Dialog mDialog;
    private String[] mTitle = {
            MyApplication.APP_CONTEXT.getResources().getString(R.string.check_waiting_in),
            MyApplication.APP_CONTEXT.getResources().getString(R.string.check_in),
            MyApplication.APP_CONTEXT.getResources().getString(R.string.check_wait_sync)
    };
    private CheckVpAdapter vpAdapter;
    private FragmentManager fm;
    public CheckView(Fragment fragment,View view) {
        this.fragment = fragment;
        ButterKnife.bind(this,view);
        initView();
    }

    private void initView() {
        //在fragment中设置viewpager的adapter的时候，需要调用fragment的getChildFragmentManager
        fm = fragment.getChildFragmentManager();
        vpAdapter = new CheckVpAdapter(fm,mTitle);
        vp_check.setAdapter(vpAdapter);
        tb_check.setupWithViewPager(vp_check);
        tb_check.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                vp_check.setCurrentItem(tab.getPosition());
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
    }

    public void showProgressDialog(String msg){
        mDialog = CustomProgressDialog.createLoadingDialog(fragment.getActivity(), msg);
        mDialog.setCancelable(false);
        mDialog.show();
    }

    public void hideProgressDialog(){
        if(mDialog!=null){
            mDialog.cancel();
        }
    }

    public void showToast(String msg){
        ToastUtil.toastShort(msg);
    }

    public void setCurrentFragment(int index) {
        vp_check.setCurrentItem(index);
    }

    public  void refreshView() {
        //重新显示CheckFragment
        Fragment checkFragment = new CheckFragment();
        FragmentManager fragmentManager = fragment.getActivity().getSupportFragmentManager();
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        transaction.replace(R.id.frame_business, checkFragment);
        transaction.commit();
    }
}
